package net.sapfree.core.db;

import java.io.InputStream;
import java.io.Reader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Map;

import net.sapfree.core.util.format.ValueObjectFormat;

public class MysqlDataBaseExecutor  extends DataBaseExecutor{
	protected MysqlDataBaseExecutor(Connection conn){ super(conn); }
	protected Record fillRecord(ResultSet rs, ResultSetMetaData rsmd, Map<String, String> entryColumnType) throws SQLException 
	{
		Record record = new Record();
//		record.setColumnType(entryColumnType);
		int columnCount = rsmd.getColumnCount();
		for (int i = 1; i <= columnCount; i++)
		{
			Object object = null;
			String columnName = rsmd.getColumnName(i).toUpperCase();
			switch(rsmd.getColumnType(i))
			{
				default		:object = rs.getObject(columnName);
							 break;
			}
			record.addData(columnName, object);
		}
		return record;
	}

	protected void setPreparedStatementParameter(PreparedStatement pstmt, int paramIndex, int columnTypeCode, Object paramValue, ValueObjectFormat formatter) throws SQLException
	{ 
		switch(columnTypeCode)
		{
			//BIT(yes/no,true/false,on/off)  java.lang.Boolean
			case -7:	pstmt.setBoolean(paramIndex, formatter.getJavaBoolean(paramValue));
						break;
			
			//LONGBINARY(ole)   byte[]
			case -4:	InputStream is = formatter.getInputStream(paramValue);
						int iStreamLength = formatter.getLength(paramValue);
						pstmt.setBinaryStream(paramIndex, is, iStreamLength);
						break;
		
			//LONGCHAR(memo) java.lang.String
			case -1:	Reader reader = formatter.getReader(paramValue);
						int iLength = formatter.getLength(paramValue);
						pstmt.setCharacterStream(paramIndex, reader, iLength);
						break;
						//VARCHAR(text) java.lang.String
			case 1:	pstmt.setString(paramIndex, (String)paramValue);
						break;		
			//DECIMAL(decimal)   java.math.BigDecimal
			case 2:		pstmt.setBigDecimal(paramIndex, formatter.getBigDecimal(paramValue));
						break;
						
						//INTEGER(long,counter)  java.lang.Integer
			case 4:		pstmt.setInt(paramIndex, formatter.getInt(paramValue));
						break;
			
			//SMALLINT(int)	java.lang.Short
			case 5:		pstmt.setShort(paramIndex, formatter.getShort(paramValue));
						break;
			
			//REAL(float) java.lang.Float
			case 7:		pstmt.setFloat(paramIndex, formatter.getFloat(paramValue));
						break;

			//DOUBLE(double) java.lang.Double
			case 8:		pstmt.setDouble(paramIndex, formatter.getDouble(paramValue));
						break;
			
			//VARCHAR(text) java.lang.String
			case 12:	pstmt.setString(paramIndex, (String)paramValue);
						break;
						//DATE   java.sql.Timestamp
			case 91:	pstmt.setString(paramIndex, (String)paramValue);
						break;
			//DATETIME(datetime,date(l/m/s),time(l/m/s))  java.sql.Timestamp
			case 93:	pstmt.setTimestamp(paramIndex, formatter.getJavaSqlTimestamp(paramValue,null));
						break;

			default:    throw new RuntimeException("one oracle column type in used is not supported! you can use the following type : CHAR/NUMBER/VARCHAR2/DATE/TIMESTAMP/BLOB/CLOB.");
		}
	}
}
